package com.xkdx.caipiao;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.xkdx.caipiao.module.network.AbsModule;
import com.xkdx.caipiao.my.MyDetailActivity;
import com.xkdx.caipiao.presistence.historyquery.HistoryQueryAction;
import com.xkdx.caipiao.presistence.historyquery.HistoryQueryInfo;
import com.xkdx.caipiao.presistence.historyquery.HistoryQueryModule1;
import com.xkdx.caipiao.presistence.historyquery.HistoryQueryPresistence1;
import com.xkdx.caipiao.presistence.hongbao.HongbaoAction;
import com.xkdx.caipiao.presistence.hongbao.HongbaoInfo;
import com.xkdx.caipiao.presistence.hongbao.HongbaoModule;
import com.xkdx.caipiao.presistence.hongbao.HongbaoPresistence;
import com.xkdx.caipiao.redpacke.BuyRedpacketList;
import com.xkdx.caipiao.redpacke.RedPackeDetail;
import com.xkdx.caipiao.setup.ShowIdentityInfo;
import com.xkdx.caipiao.statics.IConstants;
import com.xkdx.caipiao.util.Md5Utils;
import com.xkdx.guangguang.InfoNotificationActivity;
import com.xkdx.guangguang.LoginActivity;
import com.xkdx.guangguang.MyAttentionActivity;
import com.xkdx.guangguang.MyScoreActivity;
import com.xkdx.guangguang.MySetupActivity;
import com.xkdx.guangguang.fragment.user.UserSharePrefence1;
import com.xkdx.guangguang.util.SharePrefenceUtil;

public class MyStrollActivity extends ThreadActivity implements OnClickListener {
    private TextView tv_name, tv_wanshan, tv_balance, tv_red_packet;
    private LinearLayout btn_success_chongzhi, btn_success_tixian, red_packe,
            ll_dingdan, ll_myaccount, ll_myred, ll_myid, ll_set;
    private static String TAG = "UserInfoFragment";
    private SharePrefenceUtil su;
    private UserSharePrefence1 usrInfo;
    public static boolean isUpdate = false;
    private static Context context;
    View view;
    TextView title_info, red_detail, buy_lottery_tip, account, tv_score;
    ViewPager viewPager;
    LinearLayout ll;
    // public static LoginInfo loginInfo;
    private View myAttionBar;
    private View myMsgBar;
    private View myQRBar;

    HistoryQueryAction action;
    HistoryQueryModule1 module;
    HistoryQueryPresistence1 presistence;

    ListView lv_all, lv_no_open, lv_win;
    View view_all, view_no_open, view_win;
    List<View> viewList;
    List<HistoryQueryInfo> list, list_win, list_no_open;
    List<HongbaoInfo> list1;

    Drawable im_bottom;
    TextView tv_all, tv_win, tv_no_open, yuan;
    Button btn_recharge, btn_withdraw_cash;
    //    LoginInfo info;
    HongbaoInfo info1;
    float money;

    Button redpacke;
    int clickpage;
    private boolean isRedPaketRequest;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stroll_caipiao);
        usrInfo = new UserSharePrefence1(this, IConstants.SP_USER_NEW);
        findView();
//        startThread();
//        initData();
    }

    private void findView() {
        tv_balance = (TextView) findViewById(R.id.balance);
        tv_red_packet = (TextView) findViewById(R.id.red_packet);
        tv_name = (TextView) findViewById(R.id.login_success_name);
        tv_wanshan = (TextView) findViewById(R.id.login_success_wanshan);
        tv_score = (TextView) findViewById(R.id.score);
        btn_success_chongzhi = (LinearLayout) findViewById(R.id.btn_success_chongzhi);
        btn_success_tixian = (LinearLayout) findViewById(R.id.btn_success_tixian);
        red_packe = (LinearLayout) findViewById(R.id.red_packe);
        ll_dingdan = (LinearLayout) findViewById(R.id.ll_dingdan);
        ll_myaccount = (LinearLayout) findViewById(R.id.ll_myaccount);
        ll_myred = (LinearLayout) findViewById(R.id.ll_myred);
        ll_myid = (LinearLayout) findViewById(R.id.ll_myid);
        ll_set = (LinearLayout) findViewById(R.id.ll_set);
        myAttionBar = findViewById(R.id.ll_mycare);
        myMsgBar = findViewById(R.id.ll_mymsg);
        myQRBar = findViewById(R.id.ll_scan);
        findViewById(R.id.score_bar).setOnClickListener(this);
        myAttionBar.setOnClickListener(this);
        myMsgBar.setOnClickListener(this);
        myQRBar.setOnClickListener(this);
        ll_dingdan.setOnClickListener(this);
        ll_myaccount.setOnClickListener(this);
        ll_myred.setOnClickListener(this);
        ll_myid.setOnClickListener(this);
        ll_set.setOnClickListener(this);
        btn_success_chongzhi.setOnClickListener(this);
        btn_success_tixian.setOnClickListener(this);
        red_packe.setOnClickListener(this);

        setTextLine((TextView) findViewById(R.id.red_packet_lab));
        setTextLine((TextView) findViewById(R.id.balance_lab));
        setTextLine((TextView) findViewById(R.id.score_lab));
    }

    private void initData() {
        //完善信息
        if (usrInfo.getVerify().equals("1")) {
            tv_wanshan.setVisibility(View.GONE);
        } else {
            tv_wanshan.setVisibility(View.VISIBLE);
            tv_wanshan.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent3 = new Intent(MyStrollActivity.this,
                            CompleteUserInfoActivity.class);
                    startActivity(intent3);
                }
            });
            setTextLine(tv_wanshan);
        }
        //昵称
        String nick = ("").equals(usrInfo.getUserNickName()) ? usrInfo
                .getUserLoginName() : usrInfo.getUserNickName();
        tv_name.setText(nick);
        //红包
        String redPacket = usrInfo.getRedpacket().equals("") ? "0" : usrInfo.getRedpacket();
        tv_red_packet.setText(redPacket + "个");
        setTextLine(tv_red_packet);

        //余额
        /*if (IConstants.localUserMoney != 0) {
            DecimalFormat df = new DecimalFormat("#0.00");
            String st = df.format(IConstants.localUserMoney);
            tv_balance.setText(st+"元");

        } else {*/
        if (!(TextUtils.isEmpty(usrInfo.getMoney()))) {

            float money = Float.valueOf(usrInfo.getMoney()) / 100;
            DecimalFormat df = new DecimalFormat("#0.00");
            String st = df.format(money);
            if ("0.00".equals(st)) {
                tv_balance.setText("0元");
            } else {
                tv_balance.setText(st+"元");
            }

        } else {
            tv_balance.setText("0元");
        }
//        }
        setTextLine(tv_balance);

        //积分
        String score = usrInfo.getScore();
        tv_score.setText(score);
        setTextLine(tv_score);
    }

    private void setTextLine(TextView tv) {
        tv.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
    }

    @Override
    public void showOnPost(HashMap<String, AbsModule> result) {
        if (result.containsKey("0")) {
        	if(module.code != null && module.code.equals("5004")) {
        		Toast.makeText(this, module.message, Toast.LENGTH_SHORT).show();
        		setLogin();
        	}
            if (isRedPaketRequest) {
                dataLoadDialog.dismiss();
                initData();
            } else {
                startRedRequest();
            }
        }
        // if (result.containsKey("0")) {
        //
        // info = getLoginInfo();
        // if (info != null) {
        // if (info.getVerify().equals("1")) {
        // tv_wanshan.setVisibility(View.INVISIBLE);
        // } else {
        // tv_wanshan.setVisibility(View.VISIBLE);
        // }
        //
        // if (IConstants.localUserMoney != 0) {
        // DecimalFormat df = new DecimalFormat("#0.00");
        // String st = df.format(IConstants.localUserMoney);
        // tv_balance.setText(st);
        //
        // } else {
        // if (!(TextUtils.isEmpty(info.getMoney()))) {
        //
        // float money = Float.valueOf(info.getMoney()) / 100;
        // DecimalFormat df = new DecimalFormat("#0.00");
        // String st = df.format(money);
        // if ("0.00".equals(st)) {
        // tv_balance.setText("0");
        // } else {
        // tv_balance.setText(st);
        // }
        //
        // } else {
        // tv_balance.setText("0");
        // }
        // }
        //
        // String nick = ("").equals(info.getUser_nickname()) ? info
        // .getUser_login_name() : info.getUser_nickname();
        //
        // tv_name.setText(nick);
        // }
        //
        // list1 = module.list1;
        // if (list1.size() > 0) {
        // tv_red_packet.setText(String.valueOf(list1.size()));
        // }
        //
        // }else {
        // SharePrefenceUtil su = new SharePrefenceUtil(MyStrollActivity.this,
        // SharePrefenceUtil.USERINFO);
        // su.clear();
        // MyLotterActivity.loginInfo = null;
        // IConstants.localUserMoney = 0;
        // Intent intent = new Intent(MyStrollActivity.this, Login.class);
        // startActivity(intent);
        // }
    }

    @Override
    protected void onResume() {
        super.onResume();
        
        if("".equals(usrInfo.getLogintoken())){
        	finish();
        }else{
        	startThread();
        }
    }

    @Override
    public void startThread() {
        isRedPaketRequest = false;
        if (!(dataLoadDialog.isShowing())) {
            dataLoadDialog.show();
            dataLoadDialog.setContentView(R.layout.progressbar_caipiao);
        }

        String md5 = usrSP.getId() + usrSP.getLogintoken() + IConstants.key;
        String signcode = Md5Utils.md5(md5);
        action = new HistoryQueryAction(usrSP.getId(), usrSP.getLogintoken(),
                "all", "1", "1000",
                signcode);
        module = new HistoryQueryModule1(this);
        presistence = new HistoryQueryPresistence1(this);
        presistence.setActitons(action);
        presistence.setModule(module);
        presistence.execute();

    }

    //红包请求
    private void startRedRequest() {
        isRedPaketRequest = true;
        if (!(dataLoadDialog.isShowing())) {
            dataLoadDialog.show();
            dataLoadDialog.setContentView(R.layout.progressbar_caipiao);
        }
        String md5 = usrSP.getId() + usrSP.getLogintoken() + IConstants.key;
        String signcode = Md5Utils.md5(md5);
        HongbaoAction action = new HongbaoAction(usrSP.getId(), usrSP.getLogintoken(), signcode);
        HongbaoModule module = new HongbaoModule(this);
        module.flag = 0;
        action.flag = 0;

        HongbaoPresistence presistence = new HongbaoPresistence(this);
        presistence.setActitons(action);
        presistence.setModule(module);
        presistence.execute();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_success_chongzhi:
                Intent inten = new Intent(this, RechargeActivity.class);
                inten.putExtra("money", tv_balance.getText().toString());
                startActivity(inten);
                break;
            case R.id.btn_success_tixian:
                Intent inten1 = new Intent(this, CashRequestActivity.class);
                startActivity(inten1);
                break;
            case R.id.red_packe:
                Intent inten2 = new Intent(this, BuyRedpacketList.class);
                startActivity(inten2);
                break;
            case R.id.ll_dingdan:
                Intent intend = new Intent(this, MyDetailActivity.class);
                startActivity(intend);
                break;
            case R.id.ll_myaccount:
                Intent intenm = new Intent(this, MyAccountNewActivity.class);
                startActivity(intenm);
                break;
            case R.id.ll_myred:
                Intent intenmy = new Intent(this, RedPackeDetail.class);
                startActivity(intenmy);
                break;
            case R.id.ll_myid:
//                su = new SharePrefenceUtil(this, SharePrefenceUtil.USERINFO);
//                if (su.getUserInfo() != null) {
//                    LoginInfo user = su.getUserInfo();
                if (usrSP != null) {
                    if (usrSP.getVerify().equals("1")) {
                        Intent identity = new Intent(this,
                                ShowIdentityInfo.class);
                        startActivity(identity);
                    } else {
                        Intent intent3 = new Intent(this,
                                CompleteUserInfoActivity.class);
                        startActivity(intent3);
                    }
//                    }

                } else {
                    Intent intent = new Intent(this, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                    startActivity(intent);
                }

                break;
            case R.id.ll_set:
                Intent intens = new Intent(this, MySetupActivity.class);
                startActivity(intens);
                break;
            case R.id.ll_mycare:
                Intent attentionIntent = new Intent(this, MyAttentionActivity.class);
                startActivity(attentionIntent);
                break;
            case R.id.ll_mymsg:
                Intent msgIntent = new Intent(this, InfoNotificationActivity.class);
                startActivity(msgIntent);
                break;
            case R.id.score_bar:
                Intent scoreIntent = new Intent(this, MyScoreActivity.class);
                startActivity(scoreIntent);
                break;
            default:
                break;
        }
    }

    @Override
    public void onBackClick(View view) {
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        startThread();
    }
}
